﻿Imports System.Globalization
Imports System.Data.OleDb
Public Class Profile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Here we can see other people's profiles.
        If Request.Params("Tourist") = User.Identity.Name Then
            btn_EditProfile.Visible = True
            btn_Galleries.Enabled = True
        Else
            btn_EditProfile.Visible = False
            btn_Galleries.Enabled = False
        End If
        If Request.Params("Tourist") = "" Then
            Response.Redirect("~/Account/Login.aspx")
        End If

        lbl_UserName.Text = Request.Params("Tourist")
        lbl_RoleName.Text = String.Join("", System.Web.Security.Roles.GetRolesForUser(Request.Params("Tourist")))
        'Populate the messages
        Comments.GetMessages(Request.Params("Tourist"), CommentsLtr)

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "SELECT * FROM FavoriteBeach_WithName WHERE Tourist = @f1"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        oleDbConn.Open()
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)

        'Getting records (only 1 line for selected tourist)
        Dim dr As OleDbDataReader = cmd.ExecuteReader()
        If dr.HasRows Then
            dr.Read()
            If Not IsDBNull(dr("Beach_FK")) Then
                lbl_NoFavBeach.Visible = False
                lbl_FavBeach.Text = dr("BeachName")
                lbl_FavBeach.Visible = True
                rpt_FavBeach.Visible = True
            End If
        End If

        'Checking if user has visited a place in greece
        Dim RegionSql As String = "SELECT * FROM Profile_Region WHERE Tourist = @f1"
        Dim cmdRegion As OleDbCommand = New OleDbCommand(RegionSql, oleDbConn)
        cmdRegion.CommandType = CommandType.Text
        cmdRegion.Parameters.AddWithValue("@f1", User.Identity.Name)
        Dim drRegion As OleDbDataReader = cmdRegion.ExecuteReader()
        Dim lbl As Label = AccordionPaneAboutMe.FindControl("lbl_Places")
        If drRegion.HasRows = True Then

            lbl.Visible = False
        Else
            lbl.Visible = True
        End If

        Dim AlbumsCountSql As String = "SELECT * FROM TotalAlbumsPerTourist WHERE Tourist = @f1"
        Dim cmdAlbumsCount As OleDbCommand = New OleDbCommand(AlbumsCountSql, oleDbConn)
        cmdAlbumsCount.CommandType = CommandType.Text
        cmdAlbumsCount.Parameters.AddWithValue("@f1", User.Identity.Name)
        Dim drAlbumsCount As OleDbDataReader = cmdAlbumsCount.ExecuteReader()
        If drAlbumsCount.HasRows = True Then
            drAlbumsCount.Read()
            If (Roles.IsUserInRole(User.Identity.Name, "Tourist")) Then

                If (drAlbumsCount("TotalAlbums") = 1) Then
                    btn_Galleries.Enabled = False
                End If
            End If
            If (Roles.IsUserInRole(User.Identity.Name, "Greece Lover")) Then
                If (drAlbumsCount("TotalAlbums") = 5) Then
                    btn_Galleries.Enabled = False
                End If
            End If
        End If
        If IsPostBack = False Then

            'Not counting activity if a tourist is visiting his own profile
            If User.Identity.Name <> Request.Params("Tourist") Then
                'Check if tourist has already viewed this profile
                Dim sqlCheckUser As String = "SELECT COUNT(*) FROM Profiles_Viewed WHERE TouristVisitor = @f1 AND ProfileVisited = @f2"
                Dim cmdCheck As OleDbCommand = New OleDbCommand(sqlCheckUser, oleDbConn)
                cmdCheck.CommandType = CommandType.Text
                cmdCheck.Parameters.AddWithValue("@f1", User.Identity.Name)
                cmdCheck.Parameters.AddWithValue("@f2", Request.Params("Tourist"))
                Dim userCount As Integer = cmdCheck.ExecuteScalar()
                If (userCount = 0) Then
                    'Counting the activity that the user has viewed this particular profile since user has not visited the profile
                    Dim ProfilesViewedSql As String = "INSERT INTO Profiles_Viewed (TouristVisitor, ProfileVisited) VALUES (@f1, @f2)"
                    Dim cmdViewed As OleDbCommand = New OleDbCommand(ProfilesViewedSql, oleDbConn)
                    cmdViewed.CommandType = CommandType.Text
                    cmdViewed.Parameters.AddWithValue("@f1", User.Identity.Name)
                    cmdViewed.Parameters.AddWithValue("@f2", Request.Params("Tourist"))
                    cmdViewed.ExecuteNonQuery()
                End If
            End If

        End If
        oleDbConn.Close()
    End Sub

    Protected Function getDateFrom(ByVal dateStr As Date) As String
        Dim retDate As String = dateStr.ToString("dd MMMM yyyy", CultureInfo.CreateSpecificCulture("en-US"))
        Return retDate
    End Function

    Protected Function getAgeFrom(ByVal dateStr As Date) As String
        Dim retDate As Integer = Convert.ToInt32(dateStr.ToString("yyyy", CultureInfo.CreateSpecificCulture("en-US")))
        Dim currentDate As Integer = DateTime.Now.Year
        Dim age As Integer = currentDate - retDate
        Return CStr(age) & " years old"
    End Function

    Protected Sub btn_Comment_Click(sender As Object, e As EventArgs) Handles btn_Comment.Click
        Dim btn As Button = DirectCast(sender, Button)
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BeachConnectionString").ConnectionString)
        Dim SqlString As String = "INSERT INTO [Personal_Messages] ([CommentsName], [Title], [TouristName], [CommentsBody], [DateAdded]) VALUES (@f1, @f2, @f3, @f4, Now())"
        oleDbConn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_Title.Text)
        cmd.Parameters.AddWithValue("@f3", Request.Params("Tourist"))
        cmd.Parameters.AddWithValue("@f4", tb_Comment.Text)
        cmd.ExecuteNonQuery()
        oleDbConn.Dispose()
        cmd.Dispose()
        Comments.GetMessages(Request.Params("Tourist"), CommentsLtr)
        tb_Comment.Text = ""
        tb_Title.Text = ""
    End Sub

    Protected Function getTouristName() As String
        Return Request.Params("Tourist")
    End Function
End Class